#include <iostream>
#include <thread>
#include <vector>
#include "lru.h"

struct MyKey {
    int id;
    bool operator==(const MyKey& other) const {
        return id == other.id;
    }
};

struct MyValue {
    int data;
    int myKeyId;
    MyKey get_key_from_val() const {
        return {myKeyId};
    }
};

// 自定义哈希函数
struct MyHash {
    std::size_t operator()(const MyKey& k) const {
        return std::hash<int>()(k.id);
    }
};

LRUCache<MyKey, MyValue, 5, MyHash> cache;

void insertThread(int startValue) {
    for (int i = startValue; i < startValue + 3; ++i) {
        MyKey key{i};
        MyValue value{i * 10};
        cache.setKey(key, value);
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}

void getThread() {
    for (int i = 0; i < 5; ++i) {
        MyKey key{rand() % 6};
        MyValue value;
        if (cache.getKey(key, value)) {
            std::cout << "Thread " << std::this_thread::get_id() << " retrieved value for key " << key.id << ": " << value.data << std::endl;
        } else {
            std::cout << "Thread " << std::this_thread::get_id() << " did not find value for key " << key.id << std::endl;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(150));
    }
}

int main() {
    std::vector<std::thread> threads;

    // 创建插入线程
    threads.push_back(std::thread(insertThread, 0));
    threads.push_back(std::thread(insertThread, 3));

    // 创建获取线程
    threads.push_back(std::thread(getThread));
    threads.push_back(std::thread(getThread));

    // 等待所有线程完成
    for (auto& t : threads) {
        if (t.joinable()) {
            t.join();
        }
    }

    return 0;
}